﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Log;
using mepostras.BLL;
using mepostras.BO.Util;
using mepostras.DAL;
using mepostras.Settings.BusinessLayer;
using GeneralCode = mepostras.BO.Util.BoConfig.MessageCode.General;
namespace mepostras.BO
{
    public class SlideBo : ISlideBo
    {
        /// <summary>
        /// Decode để hiển thị ra Html
        /// </summary>
        /// <param name="slide"></param>
        /// <returns></returns>
        private void HtmlDecode(Slide slide)
        {
            if (slide != null)
            {
                slide.Title = HttpUtility.HtmlDecode(slide.Title);
                slide.Thumbnail = HttpUtility.HtmlDecode(slide.Thumbnail);
                slide.Url = HttpUtility.HtmlDecode(slide.Url);
                slide.Description = HttpUtility.HtmlDecode(slide.Description);
            }
        }

        /// <summary>
        /// Encode để hiển thị ra Html
        /// </summary>
        /// <param name="slide"></param>
        /// <returns></returns>
        private void HtmlEncode(Slide slide)
        {
            if (slide != null)
            {
                slide.Title = HttpUtility.HtmlEncode(slide.Title);
                slide.Thumbnail = HttpUtility.HtmlEncode(slide.Thumbnail);
                slide.Url = HttpUtility.HtmlEncode(slide.Url);
                slide.Description = HttpUtility.HtmlEncode(slide.Description);
            }
        }
        
        public TList<Slide> GetSlides(string language)
        {
            int count,
                length = ServiceFacade.SettingsHelper.ArticleNewsNumber;

            var slides = DataRepository.SlideProvider.GetPaged(String.Format("Language = '{0}'", language),
                                                               "OrderIndex ASC", 0, length, out count);
            foreach (var slide in slides)
            {
                HtmlDecode(slide);
            }

            return slides;
        }

        /// <summary>
        /// Update danh sách slide
        /// </summary>
        /// <param name="slides"></param>
        /// <param name="language"></param>
        /// <param name="messageCode"></param>
        /// <returns></returns>
        public bool Update(TList<Slide> slides, string language, out string messageCode)
        {
            messageCode = GeneralCode.ServerError;
            bool result = false;
            try
            {
                // Insert file hình ảnh
                // Lấy danh sách hình ảnh có sẵn của hospital, nếu có
                for (int i = 0; i < slides.Count; i++)
                {
                    slides[i].Thumbnail = slides[i].Thumbnail.Trim();

                    // Nếu ko phải là ảnh thì next
                    if (!BoCommon.IsValidImage(slides[i].Thumbnail))
                    {
                        continue;
                    }

                    slides[i].Language = language;
                    slides[i].OrderIndex = i;
                    HtmlEncode(slides[i]);
                }

                // Xóa các file ko được chọn
                DataRepository.SlideProvider.Delete(GetSlides(language));

                // Lưu các hình ảnh mới
                DataRepository.SlideProvider.Save(slides);

                result = true;
                messageCode = GeneralCode.UpdateSuccess;
            }
            catch (Exception ex)
            {
                SingletonLogger.Instance.Error("Update", ex);
            }
            return result;
        }
    }
}
